<?php
/**
 * Created by PhpStorm.
 * User: lzhy@lzhy.me
 * Date: 2019/4/12 0012
 * Time: 13:54
 */
namespace app\admin\controller;

use think\App;
use think\Controller;
use think\facade\Request;
use app\admin\common\UTool;
use app\admin\event\Utils;
use app\admin\logic\Auth;
use app\admin\logic\Menu;

class Base extends Controller
{
    use UTool;
    public static $userId;
    public static $user;

    function __construct(App $app = null)
    {
        parent::__construct($app);
        self::$userId = session('userId');
        self::$user   = session('user');
        if(!Auth::checkAuth(self::$user)){
            if(Request::isAjax() || Request::isPost()){
                $res = ['success'=>0,'msg'=>'权限不足','_check_err'=>1];
                if(Request::param('ifr')){
                    Utils::ifrResponse($res);
                }
                exit(json_encode($res));
            }
            $this->error('权限不足','admin/index/index');
        }
        
        if(Request::isGet() && !Request::isAjax()){
            $this->assign(Menu::menu(self::$user));
            $this->assign(['user'=>self::$user]);
        }
    }

    /**
     * 构造提交表单
     * @param $title
     * @param $field
     * @param array $data
     * @param string $action
     * @param bool $modal
     * @return \think\response\View
     */
    protected function _buildEditForm($title,$field,$data = [],$action = '',$modal = false)
    {
        $data = $this->buildEditFormData($title,$field,$data,$action);
        $this->assign($data);
        if($modal){
            return view('public/modal_form');
        }
        return view('public/form');
    }

    protected function _buildModalEditForm($title,$field,$data = [],$action = '')
    {
        return $this->_buildEditForm($title,$field,$data,$action,true);
    }
}